package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckGroup;
import com.itheima.dao.CheckgroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */

@Service(interfaceClass = CheckgroupService.class)
@Transactional
public class CheckgroupServiceImpl implements CheckgroupService {
    @Autowired
    private CheckgroupDao checkgroupDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page=checkgroupDao.findPage(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(CheckGroup checkGroup, Integer[] ids) {
        checkgroupDao.addcheckGroup(checkGroup);
        Integer groupId = checkGroup.getId();
        if (ids!=null&& ids.length>0){
            for (Integer checkitemId : ids) {
            checkgroupDao.addGroupAndItem(groupId,checkitemId);
        }}
    }

    @Override
    public void delete(Integer id) {
        checkgroupDao.deleteItemIds(id);
        checkgroupDao.deleteGroup(id);
    }

    @Override
    public CheckGroup findById(Integer checkGroupId) {
        return checkgroupDao.findById(checkGroupId);
    }

    @Override
    public Integer[] findCheckitemIdsByCheckgroupId(Integer id) {
        return checkgroupDao.findCheckitemIdsByCheckgroupId(id);
    }

    @Override
    public void handleEdit(CheckGroup checkGroup, Integer[] ids) {
        Integer groupId = checkGroup.getId();
        //根据id删除关联表的检查组和检查项
        checkgroupDao.deleteItemIds(groupId);
        checkgroupDao.handleEdit(checkGroup);
        if (ids!=null&& ids.length>0){
            for (Integer checkitemId : ids) {
                checkgroupDao.addGroupAndItem(groupId,checkitemId);
            }}
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkgroupDao.findAll();
    }
}
